$(document).ready(function() {
	// 填充浏览统计数据
	$.ajax({
		url: "http://localhost:8081/category/list",
		type: "GET",
		dataType: "json",
		success: function(json) {
			$.each(json, function(i, item) {
				$('#tbody-categoris').append(
					'<tr><td>' + +item.id +
					'</td><td>' + item.name +
                    '</td><td>' + item.description +
					'</td><td>' + item.number +
                    '</td><td><button class="btn btn-success" onclick="updateCategory(' + item.id + ')"><i class="fa fa-edit"></i>编辑</button> ' +
					'<button class="btn btn-danger deleteBtn" onclick="deleteCategory(' + item.id + ')"><i class="fa fa-trash-o"></i>删除</button></td></tr>');
			});
			$('#dataTables-categoris').dataTable({
			});
		}
	});
});

// 删除按钮点击事件
function deleteCategory(id) {
	$('#confirmBtn').attr("categoryId", id);
	$('#myModal').modal();
};

// 确认删除按钮点击事件
$('#confirmBtn').click(function() {
	var id = $(this).attr("categoryId");
	$.ajax({
		type: "DELETE",
		url: "http://localhost:8081/admin/category/" + id,
		success: function(result) {
			// 刷新页面
            toastr.success(result.message);
            setTimeout(function() {
                window.location.reload();
            }, 2000)
		}
	});
});

// 增加分类按钮点击事件
$('#addCategoryBtn').click(function() {
	var categoryName = $('#addName').val();
	var categoryDescribe = $('#addDescription').val();
	var json = {
		sortName: categoryName,
        sortDescription: categoryDescribe
	};
	$.ajax({
		type: "POST",
		dataType: "json",
		contentType: "application/json;charset=utf-8",
		url: "http://localhost:8081/admin/category",
		data: JSON.stringify(json),
		success: function(result) {
			// 刷新页面
            toastr.success(result.message);
            setTimeout(function() {
                window.location.reload();
            }, 2000)
		},
		error: function() {
			location.reload();
		}
	});
});

//显示添加分类框
function addCategory() {
    // 显示模态框
    $('#addModal').modal();
}


// 编辑文章按钮点击
function updateCategory(categoryId) {
    // 往模态框填充数据
    $('#updateBtn').attr("categoryId", categoryId);
    $.ajax({
        type: "get",
        url: "http://localhost:8081/admin/category/" + categoryId,
        dataType: "json",
        async: false,
        success: function (json) {
            $('#name').val(json.name);
            $('#description').val(json.description);
        }
    });

    // 显示模态框
    $('#updateModal').modal();
}

// 更新分类按钮点击事件
$('#updateBtn').click(function () {
    var id = $('#updateBtn').attr("categoryId");
    var name = $('#name').val();
    var description = $("#description").val();
    var sort = {
        sortId: id,
        sortName: name,
        sortDescription: description
    };
    $.ajax({
        type: "PUT",
        url: "http://localhost:8081/admin/category/" + id,
        dataType: "json",
        contentType: "application/json;charset=utf-8",
        data: JSON.stringify(sort),
        success: function (result) {
            toastr.success(result.message);
            setTimeout(function() {
                window.location.reload();
            }, 2000)
        },
        error: function () {
            location.reload();
        }
    });
});
